package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.base.TbMaterial;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.TbMaterialService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tbMaterial")
public class ApiInterTbMaterialController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TbMaterial> commonApiService;
	@Autowired
	private TbMaterialService tbMaterialService;
	
	@RequiresPermissions("tbMaterial:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TbMaterial cond){
		cond.setOrderby("A.Id DESC");
		return commonApiService.searchLay(cond);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("tbMaterial:add")
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params) {
		try {
			//修改
			return tbMaterialService.add(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("tbMaterial:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(HttpServletRequest request,
			@RequestParam Map<String, Object> params){
		try {
			return tbMaterialService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tbMaterial:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TbMaterial cond) throws Exception{
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TB_Material)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TbMaterial> util = new ExportExcelUtils<TbMaterial>(new TbMaterial());
        
        String toFilePath = util.exportExcel(listData, listField, "物料名称维护");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TbMaterial cond){
		cond.setStatus(1);
		return commonApiService.searchLay(cond);
	}

}
